package com.bulletelc.apollopre.module.main.center.afterSale.detail;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.ServiceOrderGoodBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.bulletelc.apollopre.utils.LogUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CX on 2018/1/27.
 */

public class ServiceOrderDetailPresenter extends BasePresenter<ServiceOrderDetailContract.View> implements ServiceOrderDetailContract.Presenter {
    @Override
    public void getServiceOrderDetail(String id) {
        getDetail(id);
    }

    @Override
    public void postConfirm(String id) {
        memberConfirm(id);
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    /**
     * 用户确认售后
     * @param id
     */
    private void memberConfirm(String id) {
        OkGo.<String>get(NetWorkPath.POST_SERVICE_ORDER_CONFIRM+"?id="+id)
                .headers("Authorization", GlobalConfig.AUTH_HEAD + MyApplication.getToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtil.i(response.body().toString());
                        parseMemberConfirm(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorGetServiceOrderDetail(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 解析用户确认售后数据
     * @param response
     */

    private void parseMemberConfirm(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    if (isViewAttached()) {
                        getView().onSuccessPostConfirm(obj.getBoolean("data"));
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailGetServiceOrderDetail(objMeta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorGetServiceOrderDetail("数据解析错误");
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailGetServiceOrderDetail(response.body().toString());
            }
        }
    }

    private void getDetail(String id) {
        OkGo.<String>get(NetWorkPath.GET_SERVICE_ORDER_DETAIL+"?id="+id)
                .headers("Authorization", GlobalConfig.AUTH_HEAD + MyApplication.getToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseDetail(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorGetServiceOrderDetail(response.getException().toString());
                        }
                    }
                });
    }

    private void parseDetail(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    JSONObject objData = new JSONObject(obj.getString("data"));
                    JSONArray arraySub = objData.getJSONArray("product_list");
                    List<ServiceOrderGoodBean> sogList = new ArrayList<>();
                    for (int i=0;i<arraySub.length();i++) {
                        ServiceOrderGoodBean sog = new ServiceOrderGoodBean();
                        sog.setProduct_id(arraySub.getJSONObject(i).getInt("product_id"));
                        sog.setLogo_url(arraySub.getJSONObject(i).getString("logo_url"));
                        sog.setProduct_name(arraySub.getJSONObject(i).getString("product_name"));
                        sog.setProduct_sku_title(arraySub.getJSONObject(i).getString("product_sku_title"));
                        sog.setReturn_price(arraySub.getJSONObject(i).getString("return_price"));
                        sog.setQuantity(arraySub.getJSONObject(i).getInt("quantity"));
                        sogList.add(sog);
                    }
                    if (isViewAttached()) {
                        getView().onSuccessGetServiceOrderDetail(sogList, objData);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailGetServiceOrderDetail(objMeta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorGetServiceOrderDetail("数据解析错误");
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailGetServiceOrderDetail(response.body().toString());
            }
        }
    }
}
